package com.aizuda.encrypt.helper;

import cn.hutool.core.collection.CollUtil;
import com.aizuda.encrypt.entity.TableRule;
import com.aizuda.encrypt.service.ReadRemoteConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 远程配置工具包
 *
 * @author nn200433
 * @date 2024-03-27 10:54:57
 */
@Slf4j
public class RemoteConfigToolKit {

    private final List<ReadRemoteConfigService> configList;

    public RemoteConfigToolKit(List<ReadRemoteConfigService> serviceList) {
        configList = serviceList;
    }

    /**
     * 执行
     *
     * @return {@link Map }<{@link String }, {@link TableRule }>
     * @author nn200433
     */
    public Map<String, TableRule> execute(Environment env) {
        Map<String, TableRule> resultMap = new HashMap<String, TableRule>();
        if (CollUtil.isNotEmpty(configList)) {
            for (final ReadRemoteConfigService srv : configList) {
                if (null == srv) {
                    continue;
                }
                try {
                    resultMap.putAll(srv.getCfg(env));
                } catch (Exception e) {
                    log.error("", e);
                }
            }
        }
        return resultMap;
    }

}
